home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / filutil / bison110.zip / REDUCE.C < prev    next >
C/C++ Source or Header  |  1990-06-26  |  15KB  |  595 lines

  1. /* Grammar reduction for Bison.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /*
  22.  * Reduce the grammar:  Find and eliminate unreachable terminals,
  23.  * nonterminals, and productions.  David S. Bakin.
  24.  */
  25.  
  26. /*
  27.  * Don't eliminate unreachable terminals:  They may be used by the user's
  28.  * parser.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include "system.h"
  33. #include "files.h"
  34. #include "gram.h"
  35. #include "machine.h"
  36. #include "new.h"
  37.  
  38.  
  39. extern char   **tags;        /* reader.c */
  40. extern int      verboseflag;    /* getargs.c */
  41. static int      statisticsflag;    /* XXXXXXX */
  42.  
  43. #define TRUE    (1)
  44. #define FALSE    (0)
  45. typedef int bool;
  46. typedef unsigned *BSet;
  47. typedef short  *rule;
  48.  
  49.  
  50. /*
  51.  * N is set of all nonterminals which are not useless.  P is set of all rules
  52.  * which have no useless nonterminals in their RHS.  V is the set of all
  53.  * accessible symbols.
  54.  */
  55.  
  56. static BSet     N, P, V, V1;
  57.  
  58. static int      nuseful_productions, nuseless_productions,
  59.                 nuseful_nonterminals, nuseless_nonterminals;
  60.  
  61.  
  62. static void useless_nonterminals();
  63. static void inaccessable_symbols();
  64. static void reduce_grammar_tables();
  65. static void print_results();
  66. static void print_notices();
  67. void dump_grammar();
  68.  
  69. extern void fatals ();
  70.  
  71.  
  72. bool
  73. bits_equal (L, R, n)
  74. BSet L;
  75. BSet R;
  76. int n;
  77. {
  78.   int i;
  79.  
  80.   for (i = n - 1; i >= 0; i--)
  81.     if (L[i] != R[i])
  82.       return FALSE;
  83.   return TRUE;
  84. }
  85.  
  86.  
  87. int
  88. nbits (i)
  89. unsigned i;
  90. {
  91.   int count = 0;
  92.  
  93.   while (i != 0) {
  94.     i ^= (i & -i);
  95.     ++count;
  96.   }
  97.   return count;
  98. }
  99.  
  100.  
  101. int
  102. bits_size (S, n)
  103. BSet S;
  104. int n;
  105. {
  106.   int i, count = 0;
  107.  
  108.   for (i = n - 1; i >= 0; i--)
  109.     count += nbits(S[i]);
  110.   return count;
  111. }
  112.  
  113. void
  114. reduce_grammar ()
  115. {
  116.   bool reduced;
  117.  
  118.   /* Allocate the global sets used to compute the reduced grammar */
  119.  
  120.   N = NEW2(WORDSIZE(nvars), unsigned);
  121.   P = NEW2(WORDSIZE(nrules + 1), unsigned);
  122.   V = NEW2(WORDSIZE(nsyms), unsigned);
  123.   V1 = NEW2(WORDSIZE(nsyms), unsigned);
  124.  
  125.   useless_nonterminals();
  126.   inaccessable_symbols();
  127.  
  128.   reduced = (bool) (nuseless_nonterminals + nuseless_productions > 0);
  129.  
  130.   if (verboseflag)
  131.     print_results();
  132.  
  133.   if (reduced == FALSE)
  134.     goto done_reducing;
  135.  
  136.   print_notices();
  137.  
  138.   if (!BITISSET(N, start_symbol - ntokens))
  139.     fatals("Start symbol %s does not derive any sentence.",
  140.        tags[start_symbol]);
  141.  
  142.   reduce_grammar_tables();
  143.   /* if (verboseflag) {
  144.      fprintf(foutput, "REDUCED GRAMMAR\n\n");
  145.      dump_grammar();
  146.      }
  147.      */
  148.  
  149.   /**/ statisticsflag = FALSE; /* someday getopts should handle this */
  150.   if (statisticsflag == TRUE)
  151.     fprintf(stderr,
  152.         "reduced %s defines %d terminal%s, %d nonterminal%s\
  153. , and %d production%s.\n", infile,
  154.         ntokens, (ntokens == 1 ? "" : "s"),
  155.         nvars,   (nvars   == 1 ? "" : "s"),
  156.         nrules,  (nrules  == 1 ? "" : "s"));
  157.  
  158.  done_reducing:
  159.  
  160.   /* Free the global sets used to compute the reduced grammar */
  161.  
  162.   FREE(N);
  163.   FREE(V);
  164.   FREE(P);
  165.  
  166. }
  167.  
  168. /*
  169.  * Another way to do this would be with a set for each production and then do
  170.  * subset tests against N, but even for the C grammar the whole reducing
  171.  * process takes only 2 seconds on my 8Mhz AT.
  172.  */
  173.  
  174. static bool
  175. useful_production (i, N)
  176. int  i;
  177. BSet N;
  178. {
  179.   rule  r;
  180.   short n;
  181.  
  182.   /*
  183.    * A production is useful if all of the nonterminals in its RHS
  184.    * appear in the set of useful nonterminals.
  185.    */
  186.  
  187.   for (r = &ritem[rrhs[i]]; *r > 0; r++)
  188.     if (ISVAR(n = *r))
  189.       if (!BITISSET(N, n - ntokens))
  190.     return FALSE;
  191.   return TRUE;
  192. }
  193.  
  194.  
  195. /* Remember that rules are 1-origin, symbols are 0-origin. */
  196.  
  197. static void
  198. useless_nonterminals ()
  199. {
  200.   BSet Np, Ns;
  201.   int  i, n;
  202.  
  203.   /*
  204.    * N is set as built.  Np is set being built this iteration. P is set
  205.    * of all productions which have a RHS all in N.
  206.    */
  207.  
  208.   Np = NEW2(WORDSIZE(nvars), unsigned);
  209.  
  210.   /*
  211.    * The set being computed is a set of nonterminals which can derive
  212.    * the empty string or strings consisting of all terminals. At each
  213.    * iteration a nonterminal is added to the set if there is a
  214.    * production with that nonterminal as its LHS for which all the
  215.    * nonterminals in its RHS are already in the set.  Iterate until the
  216.    * set being computed remains unchanged.  Any nonterminals not in the
  217.    * set at that point are useless in that they will never be used in
  218.    * deriving a sentence of the language.
  219.    *
  220.    * This iteration doesn't use any special traversal over the
  221.    * productions.  A set is kept of all productions for which all the
  222.    * nonterminals in the RHS are in useful.  Only productions not in
  223.    * this set are scanned on each iteration.  At the end, this set is
  224.    * saved to be used when finding useful productions: only productions
  225.    * in this set will appear in the final grammar.
  226.    */
  227.  
  228.   n = 0;
  229.   while (1)
  230.     {
  231.       for (i = WORDSIZE(nvars) - 1; i >= 0; i--)
  232.     Np[i] = N[i];
  233.       for (i = 1; i <= nrules; i++)
  234.     {
  235.       if (!BITISSET(P, i))
  236.         {
  237.           if (useful_production(i, N))
  238.         {
  239.           SETBIT(Np, rlhs[i] - ntokens);
  240.           SETBIT(P, i);
  241.         }
  242.         }
  243.     }
  244.       if (bits_equal(N, Np, WORDSIZE(nvars)))
  245.     break;
  246.       Ns = Np;
  247.       Np = N;
  248.       N = Ns;
  249.     }
  250.   FREE(N);
  251.   N = Np;
  252. }
  253.  
  254. static void
  255. inaccessable_symbols ()
  256. {
  257.   BSet  Vp, Vs, Pp;
  258.   int   i, n;
  259.   short t;
  260.   rule  r;
  261.  
  262.   /*
  263.    * Find out which productions are reachable and which symbols are
  264.    * used.  Starting with an empty set of productions and a set of
  265.    * symbols which only has the start symbol in it, iterate over all
  266.    * productions until the set of productions remains unchanged for an
  267.    * iteration.  For each production which has a LHS in the set of
  268.    * reachable symbols, add the production to the set of reachable
  269.    * productions, and add all of the nonterminals in the RHS of the
  270.    * production to the set of reachable symbols.
  271.    *
  272.    * Consider only the (partially) reduced grammar which has only
  273.    * nonterminals in N and productions in P.
  274.    *
  275.    * The result is the set P of productions in the reduced grammar, and
  276.    * the set V of symbols in the reduced grammar.
  277.    *
  278.    * Although this algorithm also computes the set of terminals which are
  279.    * reachable, no terminal will be deleted from the grammar. Some
  280.    * terminals might not be in the grammar but might be generated by
  281.    * semantic routines, and so the user might want them available with
  282.    * specified numbers.  (Is this true?)  However, the nonreachable
  283.    * terminals are printed (if running in verbose mode) so that the user
  284.    * can know.
  285.    */
  286.  
  287.   Vp = NEW2(WORDSIZE(nsyms), unsigned);
  288.   Pp = NEW2(WORDSIZE(nrules + 1), unsigned);
  289.  
  290.   /* If the start symbol isn't useful, then nothing will be useful. */
  291.   if (!BITISSET(N, start_symbol - ntokens))
  292.     goto end_iteration;
  293.  
  294.   SETBIT(V, start_symbol);
  295.  
  296.   n = 0;
  297.   while (1)
  298.     {
  299.       for (i = WORDSIZE(nsyms) - 1; i >= 0; i--)
  300.     Vp[i] = V[i];
  301.       for (i = 1; i <= nrules; i++)
  302.     {
  303.       if (!BITISSET(Pp, i) && BITISSET(P, i) &&
  304.           BITISSET(V, rlhs[i]))
  305.         {
  306.           for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  307.         {
  308.           if (ISTOKEN(t = *r)
  309.               || BITISSET(N, t - ntokens))
  310.             {
  311.               SETBIT(Vp, t);
  312.             }
  313.         }
  314.           SETBIT(Pp, i);
  315.         }
  316.     }
  317.       if (bits_equal(V, Vp, WORDSIZE(nsyms)))
  318.     {
  319.       break;
  320.     }
  321.       Vs = Vp;
  322.       Vp = V;
  323.       V = Vs;
  324.     }
  325.  end_iteration:
  326.  
  327.   FREE(V);
  328.   V = Vp;
  329.  
  330.   /* Tokens 0, 1, and 2 are internal to Bison.  Consider them useful. */
  331.   SETBIT(V, 0);            /* end-of-input token */
  332.   SETBIT(V, 1);            /* error token */
  333.   SETBIT(V, 2);            /* illegal token */
  334.  
  335.   FREE(P);
  336.   P = Pp;
  337.  
  338.   nuseful_productions = bits_size(P, WORDSIZE(nrules + 1));
  339.   nuseless_productions = nrules - nuseful_productions;
  340.  
  341.   nuseful_nonterminals = 0;
  342.   for (i = ntokens; i < nsyms; i++)
  343.     if (BITISSET(V, i))
  344.       nuseful_nonterminals++;
  345.   nuseless_nonterminals = nvars - nuseful_nonterminals;
  346.  
  347.   /* A token that was used in %prec should not be warned about.  */
  348.   for (i = 1; i < nrules; i++)
  349.     if (rprecsym[i] != 0)
  350.       SETBIT(V1, rprecsym[i]);
  351. }
  352.  
  353. static void
  354. reduce_grammar_tables ()
  355. {
  356. /* This is turned off because we would need to change the numbers
  357.    in the case statements in the actions file.  */
  358. #if 0
  359.   /* remove useless productions */
  360.   if (nuseless_productions > 0)
  361.     {
  362.       short np, pn, ni, pi;
  363.  
  364.       np = 0;
  365.       ni = 0;
  366.       for (pn = 1; pn <= nrules; pn++)
  367.     {
  368.       if (BITISSET(P, pn))
  369.         {
  370.           np++;
  371.           if (pn != np)
  372.         {
  373.           rlhs[np] = rlhs[pn];
  374.           rline[np] = rline[pn];
  375.           rprec[np] = rprec[pn];
  376.           rassoc[np] = rassoc[pn];
  377.           rrhs[np] = rrhs[pn];
  378.           if (rrhs[np] != ni)
  379.             {
  380.               pi = rrhs[np];
  381.               rrhs[np] = ni;
  382.               while (ritem[pi] >= 0)
  383.             ritem[ni++] = ritem[pi++];
  384.               ritem[ni++] = -np;
  385.             }
  386.         } else {
  387.           while (ritem[ni++] >= 0);
  388.         }
  389.         }
  390.     }
  391.       ritem[ni] = 0;
  392.       nrules -= nuseless_productions;
  393.       nitems = ni;
  394.  
  395.       /*
  396.        * Is it worth it to reduce the amount of memory for the
  397.        * grammar? Probably not.
  398.        */
  399.  
  400.     }
  401. #endif /* 0 */
  402.   /* Disable useless productions,
  403.      since they may contain useless nonterms
  404.      that would get mapped below to -1 and confuse everyone.  */
  405.   if (nuseless_productions > 0)
  406.     {
  407.       int pn;
  408.  
  409.       for (pn = 1; pn <= nrules; pn++)
  410.     {
  411.       if (!BITISSET(P, pn))
  412.         {
  413.           rlhs[pn] = -1;
  414.         }
  415.     }
  416.     }
  417.  
  418.   /* remove useless symbols */
  419.   if (nuseless_nonterminals > 0)
  420.     {
  421.  
  422.       int    i, n;
  423. /*      short  j; JF unused */
  424.       short *nontermmap;
  425.       rule   r;
  426.  
  427.       /*
  428.        * create a map of nonterminal number to new nonterminal
  429.        * number. -1 in the map means it was useless and is being
  430.        * eliminated.
  431.        */
  432.  
  433.       nontermmap = NEW2(nvars, short) - ntokens;
  434.       for (i = ntokens; i < nsyms; i++)
  435.     nontermmap[i] = -1;
  436.  
  437.       n = ntokens;
  438.       for (i = ntokens; i < nsyms; i++)
  439.     if (BITISSET(V, i))
  440.       nontermmap[i] = n++;
  441.  
  442.       /* Shuffle elements of tables indexed by symbol number.  */
  443.  
  444.       for (i = ntokens; i < nsyms; i++)
  445.     {
  446.       n = nontermmap[i];
  447.       if (n >= 0)
  448.         {
  449.           sassoc[n] = sassoc[i];
  450.           sprec[n] = sprec[i];
  451.           tags[n] = tags[i];
  452.         } else {
  453.           free(tags[i]);
  454.         }
  455.     }
  456.  
  457.       /* Replace all symbol numbers in valid data structures.  */
  458.  
  459.       for (i = 1; i <= nrules; i++)
  460.     {
  461.       rlhs[i] = nontermmap[rlhs[i]];
  462.       if (ISVAR (rprecsym[i]))
  463.         /* Can this happen?  */
  464.         rprecsym[i] = nontermmap[rprecsym[i]];
  465.     }
  466.  
  467.       for (r = ritem; *r; r++)
  468.     if (ISVAR(*r))
  469.       *r = nontermmap[*r];
  470.  
  471.       start_symbol = nontermmap[start_symbol];
  472.  
  473.       nsyms -= nuseless_nonterminals;
  474.       nvars -= nuseless_nonterminals;
  475.  
  476.       free(&nontermmap[ntokens]);
  477.     }
  478. }
  479.  
  480. static void
  481. print_results ()
  482. {
  483.   int   i;
  484. /*  short j; JF unused */
  485.   rule  r;
  486.   bool  b;
  487.  
  488.   if (nuseless_nonterminals > 0)
  489.     {
  490.       fprintf(foutput, "Useless nonterminals:\n\n");
  491.       for (i = ntokens; i < nsyms; i++)
  492.     if (!BITISSET(V, i))
  493.       fprintf(foutput, "   %s\n", tags[i]);
  494.     }
  495.   b = FALSE;
  496.   for (i = 0; i < ntokens; i++)
  497.     {
  498.       if (!BITISSET(V, i) && !BITISSET(V1, i))
  499.     {
  500.       if (!b)
  501.         {
  502.           fprintf(foutput, "\n\nTerminals which are not used:\n\n");
  503.           b = TRUE;
  504.         }
  505.       fprintf(foutput, "   %s\n", tags[i]);
  506.     }
  507.     }
  508.  
  509.   if (nuseless_productions > 0)
  510.     {
  511.       fprintf(foutput, "\n\nUseless rules:\n\n");
  512.       for (i = 1; i <= nrules; i++)
  513.     {
  514.       if (!BITISSET(P, i))
  515.         {
  516.           fprintf(foutput, "#%-4d  ", i);
  517.           fprintf(foutput, "%s :\t", tags[rlhs[i]]);
  518.           for (r = &ritem[rrhs[i]]; *r >= 0; r++)
  519.         {
  520.           fprintf(foutput, " %s", tags[*r]);
  521.         }
  522.           fprintf(foutput, ";\n");
  523.         }
  524.     }
  525.     }
  526.   if (nuseless_nonterminals > 0 || nuseless_productions > 0 || b)
  527.     fprintf(foutput, "\n\n");
  528. }
  529.  
  530. void
  531. dump_grammar ()
  532. {
  533.   int i;
  534.   rule r;
  535.  
  536.   fprintf(foutput,
  537.       "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nitems = %d\n\n",
  538.       ntokens, nvars, nsyms, nrules, nitems);
  539.   fprintf(foutput, "Variables\n---------\n\n");
  540.   fprintf(foutput, "Value  Sprec    Sassoc    Tag\n");
  541.   for (i = ntokens; i < nsyms; i++)
  542.     fprintf(foutput, "%5d  %5d  %5d  %s\n",
  543.         i, sprec[i], sassoc[i], tags[i]);
  544.   fprintf(foutput, "\n\n");
  545.   fprintf(foutput, "Rules\n-----\n\n");
  546.   for (i = 1; i <= nrules; i++)
  547.     {
  548.       fprintf(foutput, "%-5d(%5d%5d)%5d : (@%-5d)",
  549.           i, rprec[i], rassoc[i], rlhs[i], rrhs[i]);
  550.       for (r = &ritem[rrhs[i]]; *r > 0; r++)
  551.     fprintf(foutput, "%5d", *r);
  552.       fprintf(foutput, " [%d]\n", -(*r));
  553.     }
  554.   fprintf(foutput, "\n\n");
  555.   fprintf(foutput, "Rules interpreted\n-----------------\n\n");
  556.   for (i = 1; i <= nrules; i++)
  557.     {
  558.       fprintf(foutput, "%-5d  %s :", i, tags[rlhs[i]]);
  559.       for (r = &ritem[rrhs[i]]; *r > 0; r++)
  560.     fprintf(foutput, " %s", tags[*r]);
  561.       fprintf(foutput, "\n");
  562.     }
  563.   fprintf(foutput, "\n\n");
  564. }
  565.  
  566.  
  567. static void
  568. print_notices ()
  569. {
  570.   extern int fixed_outfiles;
  571.  
  572.   if (fixed_outfiles && nuseless_productions)
  573.     fprintf(stderr, "%d rules never reduced\n", nuseless_productions);
  574.  
  575.   fprintf(stderr, "%s contains ", infile);
  576.  
  577.   if (nuseless_nonterminals > 0)
  578.     {
  579.       fprintf(stderr, "%d useless nonterminal%s",
  580.           nuseless_nonterminals,
  581.           (nuseless_nonterminals == 1 ? "" : "s"));
  582.     }
  583.   if (nuseless_nonterminals > 0 && nuseless_productions > 0)
  584.     fprintf(stderr, " and ");
  585.  
  586.   if (nuseless_productions > 0)
  587.     {
  588.       fprintf(stderr, "%d useless rule%s",
  589.           nuseless_productions,
  590.           (nuseless_productions == 1 ? "" : "s"));
  591.     }
  592.   fprintf(stderr, ".\n");
  593.   fflush(stderr);
  594. }
  595.